package com.ljsw.fragment;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.ljsw.activity.R;
import com.ljsw.adapter.RenWuAdapter;
import com.ljsw.adapter.XinxiAdapter;
import com.ljsw.application.App;
import com.ljsw.dialog.LoginDialog;
import com.ljsw.entity.MessageClass;
import com.ljsw.entity.RenWuClass;
import com.ljsw.util.IBtnCallListener;

/**
 * 任务信息 fragment 视图
 * 作者: 石锚
 * 版本: 1.0
 * 日期: 2015-10-16
 */
public class RenWuXinxiFragment extends BaseFragment implements OnItemClickListener,OnClickListener{
	private LoginDialog dialog;
	private View view;
	private TextView username;//押运员
	private ListView xinxi_list;
	IBtnCallListener listener;//回调接口
//	private XinxiAdapter adapter;//任务信息适配器
	private RenWuAdapter adapter;
//	private ArrayList<MessageClass> msglist = new ArrayList<MessageClass>();//任务信息类集合
	private ArrayList<RenWuClass> renwulist = new ArrayList<RenWuClass>();
	private Handler handler;
	private String num ;//任务条数
	private EditText xinxi_edit;
	private boolean flag = false;//刷新控制
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_rwxx, container,false);
		dialog= getLoadDialog();
		flag = true;
		initView();
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				dialog.dismiss();
				switch (msg.what) {
				case 0://成功加载
					adapter.notifyDataSetChanged();
					num = renwulist.size()+"";
					listener.transfermsg(num);
					break;
				case 1://加载失败
					//弹出Dialog继续加载
					break;
				}
			}
		};
		return view;
	}
	/**
	 * 初始化控件
	 */
	public void initView(){
		xinxi_edit = (EditText) view.findViewById(R.id.xinxi_edit);
		username = (TextView) view.findViewById(R.id.username);
		xinxi_list = (ListView) view.findViewById(R.id.xinxi_list);
		xinxi_list.setOnItemClickListener(this);
		getActivity().findViewById(R.id.shuaxin).setOnClickListener(this);
		
	}
	
	@Override
	public void onStart() {
		super.onStart();
		if(renwulist.size()==0){
			loadXinxi();
		}
		if(adapter==null){
			adapter = new RenWuAdapter(getActivity(), renwulist);
		}
		Log.e("renwuxinxifragmetn","renwulist======"+renwulist.size());
		xinxi_list.setAdapter(adapter);
	}
	/**
	 * 加载任务信息数据
	 */
	public void loadXinxi(){	
		dialog.show();
		dialog.setMessage("任务信息获取中...");
		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				loadRenwu();
				if(renwulist.size()>0){
					handler.sendEmptyMessage(0);
				}else{
					handler.sendEmptyMessage(1);
				}
				
			}
		}).start();
	}
	/**
	 * 创建Dialog
	 * @return
	 */
	public LoginDialog getLoadDialog(){
		dialog = new LoginDialog(getActivity()).builder();
		return dialog;
	}
	
	/**
	 * 加载我的任务数据
	 */
	public void loadRenwu(){
//		renwulist.add(new RenWuClass("上门收款", "09-11", "河西区一线", "09:30--10:30", "天津沃尔玛超市", "天津市隆昌路11号", "半封包","待收款"));
//		renwulist.add(new RenWuClass("上门收款", "09-11", "南开区一线", "11:30--12:30", "天津家乐福凤凰店", "气象台路89号", "半封包","待收款"));
//		renwulist.add(new RenWuClass("上门收款", "09-11", "塘沽二线", "15:30--16:30", "天津海上世界", "塘沽区河北西路工农村迎宾园", "全封包","待收款"));
//		renwulist.add(new RenWuClass("上门收款", "09-11", "东丽一线", "11:30--12:30", "天津百货大楼", "东丽区福山路", "半封包","待收款"));
//		renwulist.add(new RenWuClass("上门收款", "09-11", "河西区二线", "14:30--15:30", "天津沃尔玛超市", "天津市隆昌路11号", "半封包","待收款"));
//		renwulist.add(new RenWuClass("上门收款", "09-11", "河西区三线", "16:30--17:30", "天津沃尔玛超市", "天津市隆昌路11号", "全封包","待收款"));
	}
	
	@Override
	public void onAttach(Activity activity) {
		try {
			listener = (IBtnCallListener) activity;
		} catch (Exception e) {
		}
		super.onAttach(activity);
	}
	/**
	 * 跳转另外一个fragment
	 */
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        StartFwFragment fragment = new StartFwFragment();
        Bundle bundle = new Bundle();  
        bundle.putSerializable("msg", (Serializable) renwulist.get(arg2));  
        fragment.setArguments(bundle);
        ft.addToBackStack(null);
//        ft.replace(R.id.main_frame, fragment).commit();//原版代码
        ft.add(R.id.main_frame, fragment).commit();
	}
	/**
	 * 点击刷新
	 */
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if(flag){
			loadXinxi();
		}
	}
	@Override
	public void onStop() {
		flag = false;
		super.onStop();
	}
}
